package acm.蓝桥1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public static int n, m;
    public static int[] id;
    public static ArrayList<edge> list = new ArrayList<edge>();
    public static HashSet<Integer> set = new HashSet<Integer>();

    static class MyComparator implements Comparator<edge> {
        public int compare(edge arg0, edge arg1) {
            if(arg0.t > arg1.t)
                return 1;
            else if(arg0.t < arg1.t)
                return -1;
            return 0;
        }
    }

    static class edge {
        public int a;
        public int b;
        public int t;

        public edge(int a, int b, int t) {
            this.a = a;
            this.b = b;
            this.t = t;
        }
    }

    public static int find(int a) {
        int root = a;
        while(id[root] >= 0) {
            root = id[root];
        }
        int i = 0, k = a;
        while(k != root) {
            i = id[k];
            id[k] = root;
            k = i;
        }
        return root;
    }

    public static void  union(int a, int b) {
        int rootA = find(a);
        int rootB = find(b);
        if(rootA == rootB)
            return;
        int num = id[rootA] + id[rootB];
        if(id[rootA] < id[rootB]) {
            id[rootB] = rootA;
            id[rootA] = num;
        } else {
            id[rootA] = rootB;
            id[rootB] = num;
        }
    }

    public static void  kruskal() {
        Collections.sort(list, new MyComparator());
        id = new int[n + 1];
        for(int i = 1;i <= n;i++)
            id[i] = -1;
        int count = 0;
        for(int i = 0;i < list.size();i++) {
            edge p = list.get(i);
            if(find(p.a) != find(p.b)) {
                set.add(p.t);
                union(p.a, p.b);
                count++;
                if(count == n - 1)
                    break;
            }
        }
        System.out.println(set.size());
    }

    public static void main(String[] args) {
        Reader in = new Reader();
        n = in.nextInt();
        m = in.nextInt();
        for(int i = 0;i < m;i++) {
            int a = in.nextInt();
            int b = in.nextInt();
            int t = in.nextInt();
            list.add(new edge(a, b, -1 * t));
        }
        kruskal();
    }

    static
    class Reader {
        private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer tokenizer = new StringTokenizer("");

        private String innerNextLine() {
            try {
                return reader.readLine();
            } catch (IOException ex) {
                return null;
            }
        }

        public boolean hasNext() {
            while (!tokenizer.hasMoreTokens()) {    //内存中没有
                String nextLine = innerNextLine();
                if (nextLine == null) {     //且没有下一行
                    return false;
                }
                tokenizer = new StringTokenizer(nextLine);  //读入一行
            }
            return true;
        }

        public String nextLine() {
            tokenizer = new StringTokenizer("");    //清空内存中的字符
            return innerNextLine();
        }
        public void clear(){
            tokenizer = new StringTokenizer("");
        }

        public String next() {
            hasNext();
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble(){
            return Double.parseDouble(next());
        }
//    public BigInteger nextBigInt() {
//        return new BigInteger(next());
//    }
    }

}
